package com.aaa.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.aaa.constants.MyConstants;
import com.aaa.dao.UserDao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2024/7/16 14:15
 * @description：用户dao接口的实现类
 * @modified By：
 * @version: 1.0
 */
public class UserDaoImpl implements UserDao {


    @Override
    public boolean addUser(Map map) {
        //构建一个Entity
        cn.hutool.db.Entity E = cn.hutool.db.Entity.create("im_user");
        E.set("user_name", map.get("user_name"))
                .set("nick_name", map.get("nick_name"))
                .set("head_image", map.get("head_image"))
                .set("head_image_thumb", map.get("head_image_thumb"))
                .set("password", map.get("password"))
                .set("sex", map.get("sex"))
                .set("type", map.get("type"))
                .set("signature", map.get("signature"))
                .set("created_time", map.get("created_time"))
                .set("last_login_time", map.get("last_login_time")
                );
        try {
            int insert = Db.use().insert(E);
            if (insert > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean deleteUser(Map map) {
        try {
            int delete = Db.use().del(Entity.create("im_user").set("id", map.get("id")));
            if (delete > 0) {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean updateUser(Map map) {
        try {
            int update = Db.use().update(Entity.create("im_user").set("user_name", map.get("user_name"))
                    .set("nick_name", map.get("nick_name"))
                    .set("head_image", map.get("head_image"))
                    .set("head_image_thumb", map.get("head_image_thumb"))
                    .set("password", map.get("password"))
                    .set("sex", map.get("sex"))
                    .set("type", map.get("type"))
                    .set("signature", map.get("signature"))
                    .set("created_time", map.get("created_time"))
                    .set("last_login_time", map.get("last_login_time")), Entity.create("im_user").set("id", map.get("id")));
            if (update > 0) {
                return true;
            }
        } catch (SQLException e) {
        }
        return false;
    }
    @Override
    public boolean updateUserLoginTime(Map map) {
        try {
            int update = Db.use().update(Entity.create("im_user")
                    .set("last_login_time", map.get("last_login_time")), Entity.create("im_user").set("user_name", map.get("user_name")));
            if (update > 0) {
                return true;
            }
        } catch (SQLException e) {
        }
        return false;
    }


    @Override
    public Entity findUserById(Map map) {
        try {
            Entity selectOne = Db.use().queryOne("select * from im_user where id=?", map.get("id"));
            return selectOne;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * @param map
     * @return boolean
     * @create by: Teacher陈
     * @description: 根据用户id，修改type的值
     * @create time: 2024/7/18 16:33
     */
    @Override
    public boolean updateUserTypeById(Map map) {
        try {
            int update = Db.use().update(Entity.create("im_user")
                    .set("type", MyConstants.USER_TYPE_DELETE
                    ), Entity.create("im_user").set("id", map.get("id")));
            if (update > 0) {
                return true;
            }
        } catch (SQLException e) {
        }
        return false;
    }
    /**
     * @create by: Teacher陈
     * @description: 根据用户id删除朋友关系
     * @create time: 2024/7/18 16:47
     * @param map
     * @return void
     */
    @Override
    public void deleteFriendById(Map map) {

        try {
            Db.use().execute("delete from im_friend where user_id=? or friend_id=?", map.get("id"), map.get("id"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean updateOwnerId(Map map) {
        //判断用户关联的群是否只有一个人
        List<Entity> groupList = null;
        try {
            groupList = Db.use().query("select * from im_group where owner_id=?", map.get("id"));
            //如果是群主
            if(groupList.size()>0){
                for (Entity group : groupList) {
                    List<Entity> memberList = Db.use().query("select * from im_group_member where group_id=?", group.get("id"));
                    if(memberList.size()>1){
                        //将群的拥有者设置为下一个人
                        Db.use().execute("update im_group set owner_id=? where id=?", memberList.get(1).get("user_id"), group.get("id"));
                    }else{
                        //删除群
                        Db.use().execute("delete from im_group where id=?", group.get("id"));
                    }
                }
            }
                //不是群主的情况，将自己在im_group_member中的quit设置为1
           Db.use().execute("update im_group_member set quit="+ MyConstants.GROUP_MEMBER_QUIT+" where user_id=?", map.get("id"));

            return true;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public Entity findUserByName(String user_name) {
        try {
            //type=2说明是管理员
            Entity selectOne = Db.use().queryOne("select * from im_user where type=2  and  user_name=?", user_name);
            return selectOne;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public Entity findUserByNameCheck(String user_name) {
        try {
            //type=2说明是管理员
            Entity selectOne = Db.use().queryOne("select * from im_user where   user_name=?", user_name);
            return selectOne;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Entity> findUser() {
        try {
            List<Entity> select = Db.use().query("select * from im_user where type<> 3");
            return select;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
